home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / TECHNICA / AUTOCAD / H107.ZIP / RENAME.ZIP / RENAME.LSP
Lisp/Scheme  |  1991-05-13  |  9KB  |  288 lines

  1. ;;;   Rename.lsp
  2. ;;;   Copyright (C) 1991 by Autodesk, Inc.
  3. ;;;  
  4. ;;;   THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. 
  5. ;;;   ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF 
  6. ;;;   MERCHANTABILITY ARE HEREBY DISCLAIMED.
  7. ;;; 
  8. ;;;   by Jan S. Yoder
  9. ;;;   May 1991
  10. ;;;
  11. ;;;--------------------------------------------------------------------------;
  12. ;;; DESCRIPTION
  13. ;;;   
  14. ;;;   This routine allows you to rename multiple objects from a single symbol
  15. ;;;   table by specifying a name with wildcards (only "*" allowed at the
  16. ;;;   moment).  The interface is the same as that for AutoCAD's RENAME
  17. ;;;   command with the exception of allowing asteriks in the old and new
  18. ;;;   names.  A typical example follows.
  19. ;;;
  20. ;;;   Command: RENAME
  21. ;;;   RENAME: Block/Dimstyle/LAyer/LType/Style/Ucs/VIew/VPort: la
  22. ;;;   Old name: wall*
  23. ;;;   New name: fl_1_*
  24. ;;;
  25. ;;;   LAYER name changed from WALL1 to FL_1_1.
  26. ;;;   LAYER name changed from WALL2 to FL_1_2.
  27. ;;;   Command:
  28. ;;;
  29. ;;;   CAUTION:
  30. ;;;   The AutoCAD RENAME command is UNDEFINED by loading this routine.  It is
  31. ;;;   NOT REDEFINED!
  32. ;;;
  33. ;;;--------------------------------------------------------------------------;
  34.  
  35. (defun rename (/ rn_err rn_oe rn_oc tlold tlnew *STR_TOK*)
  36.   (setq rn_ver "1.00")                ; Reset this local if you make a change.
  37.   ;;
  38.   ;; Internal error handler defined locally
  39.   ;;
  40.  
  41.   (defun rn_err (s)                   ; If an error (such as CTRL-C) occurs
  42.                                       ; while this command is active...
  43.     (if (/= s "Function cancelled")
  44.       (if (= s "quit / exit abort")
  45.         (princ)
  46.         (princ (strcat "\nError: " s))
  47.       )
  48.     )
  49.     (command "undo" "end")
  50.     (if rn_oe                         ; If an old error routine exists
  51.       (setq *error* rn_oe)            ; then, reset it 
  52.     )
  53.     (setvar "cmdecho" rn_oc)          ; Reset command echoing on error
  54.     (princ)
  55.   )
  56.   
  57.   ;;
  58.   ;; Body of LLOAD function
  59.   ;;
  60.   
  61.   (if *error*                         ; Set our new error handler
  62.     (setq rn_oe *error* *error* rn_err) 
  63.     (setq *error* rn_err) 
  64.   )
  65.   (setq rn_oc (getvar "cmdecho"))     ; Save current state of command echoing
  66.   (setvar "cmdecho" 0)                ; Turn off command echoing
  67.   (command "undo" "group")            ; Start an UNDO group
  68.  
  69.   (rename_all)
  70.  
  71.   (setvar "cmdecho" rn_oc)            ; Reset command echoing
  72.   (princ)
  73. )
  74.  
  75. (defun rename_all (/ which old new)
  76.   (initget "Block Dimstyle LAyer LType Style Ucs VIew VPort")
  77.   (setq which (getkword (strcat
  78.     "\nRENAME " rn_ver ": Block/Dimstyle/LAyer/LType/Style/Ucs/VIew/VPort: "
  79.   )))
  80.   (if (or (null which) (= which ""))
  81.     (exit)
  82.     (setq which (strcase which nil))
  83.   )
  84.   (if (not (tblnext which T))
  85.     (progn
  86.       (princ (strcat "\nNo " (strcase which t) " names found."))
  87.       (exit)
  88.     )
  89.   )
  90.   (setq old (getstring "\nOld name: "))
  91.   (if (or (null old) (= old ""))
  92.     (exit)
  93.     (setq old (strcase old nil))
  94.   )
  95.   (setq new (getstring "\nNew name: "))
  96.   (if (or (null new) (= new ""))
  97.     (exit)
  98.     (setq new (strcase new nil))
  99.   )
  100.   (validate_old_new old new)
  101.   (do_rename_loop which old new)
  102. )
  103.  
  104. (defun do_rename_loop (which oldname newname / cont temp old new changed)
  105.   (setq cont T
  106.         temp (tblnext which T)
  107.   )
  108.   (if temp
  109.     (while cont
  110.       (if (wcmatch (setq old (cdr(assoc 2 temp))) oldname)
  111.         (progn
  112.           (setq new (setnew old newname))
  113.           (command ".RENAME" which old new)
  114.           (princ (strcat "\n" (strcase which nil) " name changed from "
  115.                          old " to " new ". ")
  116.           )
  117.           (setq changed T)
  118.         )
  119.       )
  120.       (if (not (setq temp (tblnext which nil)))
  121.         (setq cont nil)
  122.       )
  123.     )
  124.   )
  125.   (if (not changed)
  126.     (princ (strcat "\nNo matching " (strcase which t) " names found."))
  127.   )
  128. )
  129.  
  130. (defun setnew (old new)
  131.   (setq oll (length tlold)
  132.         j   0
  133.   )
  134.   (while (< j oll)
  135.     (setq told (if (nth j tlold) (nth j tlold) ""))
  136.     (setq tnew (if (nth j tlnew) (nth j tlnew) ""))
  137.     (setq new (strrstr old told))
  138.     (setq old (substr old 1 (- (strlen old) (strlen new) (strlen told))))
  139.     (setq old (strcat old tnew new))
  140.     (setq j (1+ j))
  141.   )
  142.   old
  143. )
  144.  
  145. (defun validate_old_new (old new)
  146.   (setq cont  T
  147.         temp  (strtok old "*")
  148.         tlold (list temp)
  149.   )
  150.   (while cont
  151.     (setq temp (strtok nil "*"))
  152.     (if (null temp)
  153.       (setq cont nil)
  154.       (setq tlold (append tlold (list temp)))
  155.     )
  156.   )
  157.   (setq cont  T
  158.         temp  (strtok new "*")
  159.         tlnew (list temp)
  160.   )
  161.   (while cont
  162.     (setq temp (strtok nil "*"))
  163.     (if (null temp)
  164.       (setq cont nil)
  165.       (setq tlnew (append tlnew (list temp)))
  166.     )
  167.   )
  168.   (if (/= (length tlold) (length tlnew))
  169.     (progn
  170.       (princ "\nChange specs do not match.")
  171.       (exit)
  172.     )
  173.   )
  174. )
  175.  
  176. ;;;           
  177. ;;; STRTOK -- Searches one string for tokens, which are separated by the 
  178. ;;;           delimiters found in a second string.  String 1 contains the 
  179. ;;;           string to be tokenized on the first call to strtok; thereafter 
  180. ;;;           it should be nil for all subsequent calls to strtok for the 
  181. ;;;           same string.
  182. ;;;   
  183. ;;;           The first call to strtok returns the first token found in 
  184. ;;;           the string, as a string, and sets the value of *STR_TOK*, 
  185. ;;;           a global variable, to the remainder of the string passed in 
  186. ;;;           as the first argument. Subsequent calls to srttok with a null 
  187. ;;;           first argument will work through the string in *STR_TOK* 
  188. ;;;           until no more tokens remain.
  189. ;;;   
  190. ;;;           The separator string may be different on each call, if desired.
  191. ;;;   
  192. ;;;           The following code fragment produces the output below.
  193. ;;;   
  194. ;;;             (setq str "(defun strtok (_s1 _s2 / j s_l)") ;)
  195. ;;;             (print (strtok str " ()/"))
  196. ;;;             (while (setq temp (strtok nil " ()/")) (print temp))(princ)
  197. ;;;  
  198. ;;;             "defun"
  199. ;;;             "strtok" 
  200. ;;;             "_s1" 
  201. ;;;             "_s2" 
  202. ;;;             "j" 
  203. ;;;             "s_l" 
  204. ;;;           
  205. ;;;           If the first argument is not a string and the original string
  206. ;;;           has been fully tokenized, -1 is returned.  If the second 
  207. ;;;           argument is not a string, -2 is returned.
  208. ;;;   
  209. (defun strtok (_s1 _s2 / j sl s_l tok ch temp token)
  210.   (if (or (= (type _s1) 'STR) (= (type *STR_TOK*) 'STR))
  211.     (if (= (type _s2) 'STR)
  212.       (if (> (setq sl (strlen (if _s1 _s1 *STR_TOK*))) 0)
  213.         (progn
  214.           (setq j 1)
  215.           (repeat (strlen _s2)
  216.             (setq s_l (if s_l (append s_l (list (substr _s2 j 1)))
  217.                               (list (substr _s2 j 1))
  218.                       )
  219.                   j   (1+ j)
  220.             )
  221.           )
  222.           (setq j 1 tok "")
  223.           (while (and (<= j sl)
  224.                    (not (member (setq ch (substr (if _s1 _s1 *STR_TOK*) j 1)) 
  225.                               s_l)
  226.                    )
  227.                  )
  228.             (setq tok (strcat tok ch)
  229.                   j   (1+ j)
  230.             )
  231.           )
  232.           (setq temp      (if _s1 _s1 *STR_TOK*)
  233.                 *STR_TOK* (substr temp (1+ j))
  234.                 token     (substr temp 1 (1- j))
  235.           )
  236.           (if (= (strlen token) 0)    ; If no token found
  237.             (strtok nil _s2)          ; Recurse through sucessive separators
  238.             token                     ; Return new token
  239.           )
  240.         )
  241.         (setq *STR_TOK* nil)
  242.       )
  243.       -2
  244.     )
  245.     -1
  246.   )
  247. )
  248. ;;;
  249. ;;; STRRSTR -- Scans a string for the occurrence of a given substring.
  250. ;;;            Returns the remainder of the string
  251. ;;;           
  252. ;;;           If both arguments are not strings -1 is returned.
  253. ;;;
  254. (defun strrstr (_s1 _s2 / j sl sl2)
  255.   (if (and (= (type _s1) 'STR) 
  256.            (= (type _s2) 'STR)
  257.       )
  258.     (progn
  259.       (setq j   0
  260.             sl  (strlen (eval _s1))
  261.             sl2 (strlen _s2)
  262.       )
  263.       (while (< j sl)
  264.         (if (= (substr (eval _s1) (setq j (1+ j)) 1)
  265.                (substr _s2 1 1)
  266.             )
  267.           (if (= (substr (eval _s1) j sl2) _s2)
  268.             (progn
  269.               (setq _s1 (substr (eval _s1) (+ j sl2)))
  270.               (setq j sl)
  271.               (eval _s1)
  272.             )
  273.           )
  274.         )
  275.       )
  276.     )
  277.     -1
  278.   )
  279. )
  280.  
  281. (if (not undefined) (command "UNDEFINE" "RENAME"))
  282. (setq undefined T)
  283.  
  284. (defun c:rename () (rename))
  285. (princ "\n\tRENAME loaded.  Type RENAME to start program. \t")
  286. (princ)
  287.  
  288.